package com.simple.datax.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.simple.datax.bo.IDataxUserInfoBO;
import com.simple.datax.vo.DataxUserInfoVO;
import com.simple.util.common.Constants;
import com.simple.util.common.ErrorUtil;
import com.simple.util.exception.SimpleApplicationException;
import com.simple.util.object.JFValidationBean;
import com.simple.util.object.datax.DataxUserBean;

@Controller 
public class LoginController extends AbstractDataxBaseController {
	
	private final static String JSP_PATH="_Login";
	
	@Autowired
	private IDataxUserInfoBO dataxUserInfoBO;
	

	@RequestMapping("/login")  
    public String index(@RequestParam(value="loginId",required=false) String loginId,
			   @RequestParam(value="password",required=false) String password,
			   final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException  {
		final List<JFValidationBean> errorList = getErrorList();
        
        try{
        	final DataxUserInfoVO currentUserInfoVO = dataxUserInfoBO.doLogin(loginId, password);
        	final DataxUserBean currentUserBean = dataxUserInfoBO.getUserBean(currentUserInfoVO.getUserId());
        	request.getSession().setAttribute(Constants.CURRENT_SESSION_USERBEAN, currentUserBean);
        }catch (SimpleApplicationException ae) {
            errorList.add(ErrorUtil.generateJFValidationFromAppException(ae));
        } 
		
        final String returnPage;
        if(errorList.isEmpty()){
        	returnPage = "redirect:/welcome";
        }else{
        	returnPage = "forward:/index";
        }
        return returnPage;
    }
	
	@RequestMapping("/frametop")  
    public String index(final HttpServletRequest request, final HttpServletResponse response) {
		final DataxUserBean dataxUserBean = (DataxUserBean) request.getSession().getAttribute(Constants.CURRENT_SESSION_USERBEAN);
		request.setAttribute("sessionUserBean", dataxUserBean);
		return getJspPath("top");
    }
	
	@RequestMapping("/framewelcome")  
    public String weclome(final HttpServletRequest request, final HttpServletResponse response) {
		final DataxUserBean dataxUserBean = (DataxUserBean) request.getSession().getAttribute(Constants.CURRENT_SESSION_USERBEAN);
		request.setAttribute("sessionUserBean", dataxUserBean);
		return getJspPath("welcome");
    }
	
	@RequestMapping("/welcome")  
    public String loginindex(final HttpServletRequest request, final HttpServletResponse response) {
		final DataxUserBean dataxUserBean = (DataxUserBean) request.getSession().getAttribute(Constants.CURRENT_SESSION_USERBEAN);
		request.setAttribute("sessionUserBean", dataxUserBean);
		return getJspPath("login");
    }
	
	@RequestMapping("/logout")  
    public String logonout(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
		request.getSession().invalidate();
		return "redirect:/alertMessage?errorId=message.logout.success&goPage=index";
    }

	@Override
	protected String getBaseJspPath() {
		return JSP_PATH;
	}

}
